import { Component, OnInit, ElementRef, ViewChild, ViewEncapsulation } from '@angular/core';
import { AxiosService } from '../../axios.service';
import { DatePipe } from '@angular/common';

@Component({
  selector: 'app-addtask',
  templateUrl: './addtask.component.html',
  styleUrls: ['./addtask.component.scss'],
  providers: [DatePipe],

})
export class AddtaskComponent implements OnInit {
  gdname;// 治具名称
  zjtime = null;//治具交期
  yjtime = null;//预计完成时间
  sjzgs;//设计总工时
  sjjbtime;//设计基本时间
  bcjbtime;
  jgjbtime;
  erjbtime;
  zzjbtime;
  sjztime;//设总时间
  bcztime;
  jgztime;
  erztime;
  zzztime;
  sejilist = false;
  zjtypeof;//治具类型
  zjtypelist;//治具类型list
  keypolist1 = [];
  keypolist2 = [];
  keypolist3 = [];
  keypolist4 = [];
  keypolist5 = [];

  value;
  title = '請輸入治具數量'
  constructor(
    public axios: AxiosService,
    private datePipe: DatePipe,

  ) {

  }

  ngOnInit(): void {
    this.getlistdata()
  }
  getlistdata() {
    this.axios.axiosPost("QueryWorkhour").then((data => {
      if (data["result"] == "ok") {
        console.log(data)
        this.zjtypelist = [];
        for (var i = 0; i < data["order"].length; i++) {
          this.zjtypelist.push(data["order"][i].type)
        }
        console.log(this.zjtypelist)
      }
    }))
  }

  // 选择类型
  tpyeof(result: Date): void {
    console.log('onChange: ', result);
    this.axios.axiosPost("QueryWorkhour").then((data => {
      if (data["result"] == "ok") {
        for (var i = 0; i < data["order"].length; i++) {
          if (result == data["order"][i]["type"]) {
            console.log("此刻" + i)
            this.keypolist1 = data["order"][i]["arr"][0]["arr"];
            this.keypolist2 = data["order"][i]["arr"][1]["arr"];
            this.keypolist3 = data["order"][i]["arr"][2]["arr"];
            this.keypolist4 = data["order"][i]["arr"][3]["arr"];
            this.keypolist5 = data["order"][i]["arr"][4]["arr"];
            this.sjjbtime = data["order"][i]["arr"][0]["基本工時"]
            this.bcjbtime = data["order"][i]["arr"][1]["基本工時"]
            this.jgjbtime = data["order"][i]["arr"][2]["基本工時"]
            this.erjbtime = data["order"][i]["arr"][3]["基本工時"]
            this.zzjbtime = data["order"][i]["arr"][4]["基本工時"]
            this.sjztime = Number(data["order"][i]["arr"][0]["基本工時"])
            this.bcztime = Number(data["order"][i]["arr"][1]["基本工時"])
            if (this.value) {
              this.jgztime = Number(data["order"][i]["arr"][2]["基本工時"]) * this.value
              this.erztime = Number(data["order"][i]["arr"][3]["基本工時"]) * this.value
              this.zzztime = Number(data["order"][i]["arr"][4]["基本工時"]) * this.value
            } else {
              this.jgztime = 0
              this.erztime = 0
              this.zzztime = 0
            }
            console.log(this.keypolist1, this.sjjbtime)
          }
        }
      }
    }))


  }
  keypo1(key1) {
    if (this.keypolist1[key1].kg == true) {
      this.sjztime = Number(this.keypolist1[key1].value) + Number(this.sjztime)
      console.log(this.sjztime, this.keypolist1[key1].value)
    } else if (this.keypolist1[key1].kg == false) {
      this.sjztime = Number(this.sjztime) - Number(this.keypolist1[key1].value)
    }

  }
  keypo2(key2) {
    if (this.keypolist2[key2].kg == true) {
      this.bcztime = Number(this.keypolist2[key2].value) + Number(this.bcztime)
      console.log(this.bcztime, this.keypolist2[key2].value)
    } else if (this.keypolist2[key2].kg == false) {
      this.bcztime = Number(this.bcztime) - Number(this.keypolist2[key2].value)
    }

  }
  keypo3() {
    if (!this.value) {
      this.value = 1;
      this.keypo4();
      this.keypo5();
    }
    var keypopo = 0;
    for (var i = 0; i < this.keypolist3.length; i++) {
      if (this.keypolist3[i].kg == true) {
        keypopo += Number(this.keypolist3[i].value);
      }
    }
    this.jgztime = (Number(this.jgjbtime) + keypopo) * this.value

  }
  keypo4() {
    if (!this.value) {
      this.value = 1;
      this.keypo3();
      this.keypo5();
    }
    var keypopo = 0;
    for (var i = 0; i < this.keypolist4.length; i++) {
      if (this.keypolist4[i].kg == true) {
        keypopo += Number(this.keypolist4[i].value);
      }
    }
    this.erztime = (Number(this.erjbtime) + keypopo) * this.value

  }
  keypo5() {
    if (!this.value) {
      this.value = 1;
      this.keypo4();
      this.keypo3();
    }
    var keypopo = 0;
    for (var i = 0; i < this.keypolist5.length; i++) {
      if (this.keypolist5[i].kg == true) {
        keypopo += Number(this.keypolist5[i].value);
      }
    }
    this.zzztime = (Number(this.zzjbtime) + keypopo) * this.value

  }

  // 清除信息
  delInput() {
    console.log(this.zjtypeof)
    this.keypolist1 = [];
    this.keypolist2 = [];
    this.keypolist3 = [];
    this.keypolist4 = [];
    this.keypolist5 = [];
    this.jgjbtime = "";
    this.bcjbtime = "";
    this.sjjbtime = "";
    this.erjbtime = "";
    this.zzjbtime = "";
    this.sjztime = "";
    this.bcztime = "";
    this.jgztime = "";
    this.erztime = "";
    this.zzztime = "";
    this.gdname = "";
    this.zjtypeof = "";
    this.zjtime = "";
    this.yjtime = ""
    this.value = 0;
  }
  // 保存信息
  save() {
    console.log("名称" + this.gdname)
    console.log("类型" + this.zjtypeof)
    console.log("交期" + this.zjtime)
    console.log("预计完成时间" + this.yjtime)
    console.log("设计总工时" + this.sjztime)
    console.log("编程总工时" + this.bcztime)
    console.log("加工总工时" + this.jgztime)
    console.log("二次加工总工时" + this.erztime)
    console.log("组装总工时" + this.zzztime)
    if (this.gdname && this.zjtypeof && this.zjtime && this.yjtime && this.sjztime && this.bcztime && this.jgztime && this.erztime && this.zzztime && this.value) {
      this.axios.axiosPost("AddTask",
        {
          "name": this.gdname, "type": this.zjtypeof, "delivery_date": this.zjtime, "end_date": this.yjtime,
          "quatity": this.value, "design_workhour": this.sjztime, "code_workhour": this.bcztime,
          "jiagong_workhour": this.jgztime, "ercijiagong_workhour": this.erztime, "zuzhuang_workhour": this.zzztime
        }).then((data) => {
          if(data["result"]=="ok"){
            alert("添加成功")
            this.delInput();
          }else{
            alert("新增任务目前无法添加")
          }
        })

    }else{
      alert("部分必填信息未填写")
    }
  }
  // 治具交期
  onChange(date: Date): void {
    this.zjtime = date;
    this.zjtime = this.datePipe.transform(this.zjtime, 'yyyy-MM-dd');
    console.log(this.zjtime);
  }
  // 预计时间
  onChange2(date: Date): void {
    this.yjtime = date;
    this.yjtime = this.datePipe.transform(this.yjtime, 'yyyy-MM-dd');
    console.log(this.yjtime);
  }
  onChange3(value: string, zjtypeof): void {
    console.log(zjtypeof)
    if (zjtypeof) {
      this.axios.axiosPost("QueryWorkhour").then((data => {
        if (data["result"] == "ok") {
          for (var i = 0; i < data["order"].length; i++) {
            if (zjtypeof == data["order"][i]["type"]) {
              this.keypolist1 = data["order"][i]["arr"][0]["arr"];
              this.keypolist2 = data["order"][i]["arr"][1]["arr"];
              this.keypolist3 = data["order"][i]["arr"][2]["arr"];
              this.keypolist4 = data["order"][i]["arr"][3]["arr"];
              this.keypolist5 = data["order"][i]["arr"][4]["arr"];
              this.sjjbtime = data["order"][i]["arr"][0]["基本工時"]
              this.bcjbtime = data["order"][i]["arr"][1]["基本工時"]
              this.jgjbtime = data["order"][i]["arr"][2]["基本工時"]
              this.erjbtime = data["order"][i]["arr"][3]["基本工時"]
              this.zzjbtime = data["order"][i]["arr"][4]["基本工時"]
              this.sjztime = data["order"][i]["arr"][0]["基本工時"]
              this.bcztime = data["order"][i]["arr"][1]["基本工時"]
              if (this.value) {
                this.jgztime = Number(data["order"][i]["arr"][2]["基本工時"]) * this.value
                this.erztime = Number(data["order"][i]["arr"][3]["基本工時"]) * this.value
                this.zzztime = Number(data["order"][i]["arr"][4]["基本工時"]) * this.value
              } else {
                this.jgztime = 0
                this.erztime = 0
                this.zzztime = 0
              }
            }
          }
        }
      }))
    }
    this.updateValue(value);
  }
  @ViewChild('inputElement', { static: false }) inputElement: ElementRef;
  // '.' at the end or only '-' in the input box.
  onBlur(): void {
    if (this.value.charAt(this.value.length - 1) === '.' || this.value === '-') {
      this.updateValue(this.value.slice(0, -1));
    }
  }

  updateValue(value: string): void {
    const reg = /^-?(0|[1-9][0-9]*)(\[0-9]*)?$/;
    if ((!isNaN(+value) && reg.test(value)) || value === '' || value === '-') {
      this.value = value;
    }
    this.inputElement.nativeElement.value = this.value;
    this.updateTitle();
  }

  updateTitle(): void {
    this.title = (this.value !== '-' ? this.formatNumber(this.value) : '-') || '請輸入治具數量';
  }

  formatNumber(value: string): string {
    const string = `${value}`;
    const list = string.split('.');
    const prefix = list[0].charAt(0) === '-' ? '-' : '';
    let num = prefix ? list[0].slice(1) : list[0];
    let result = '';
    while (num.length > 3) {
      result = `,${num.slice(-3)}${result}`;
      num = num.slice(0, num.length - 3);
    }
    if (num) {
      result = num + result;
    }
    return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
  }

  // 人工工時選擇時間
  StartChange(date: Date): void {
    this.zjtime = date;
    this.zjtime = this.datePipe.transform(this.zjtime, 'yyyy-MM-dd');
    console.log(this.zjtime);
  }
}
